package view.servlet.ajax;


import dao.UserDao;
import dao.impl.UserDaoImpl;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/checkUsernameRegister")
public class CheckUsernameRegister extends HttpServlet {
    UserDao userDao = new UserDaoImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("UTF-8");
        String username = req.getParameter("username");
        PrintWriter out = resp.getWriter();
        if (username.length()<3||username.length()>10) {
            out.print("<font color=red>用户名为3-10位</font>");
        } else {
            if(userDao.query(username)!=null){
                out.print("<font color=red>用户名已存在</font>");
            } else{
                out.print("<font color=green>用户名合法</font>");
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
